package com.sky.controller.user;

import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "地址相关接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;
    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增地址")
    public Result insert(@RequestBody AddressBook addressBook){
        log.info("新增地址信息：{}",addressBook);
        addressBookService.save(addressBook);
        return Result.success();
    }

    /**
     * 查询当前用户的所有地址信息
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有地址")
    public Result<List<AddressBook>> list(){
        log.info("用户查询所有地址");
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(addressBook);
        return Result.success(list);
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    @ApiOperation(value = "查询默认地址")
    public Result<AddressBook> defaultAddress(){
        log.info("查询用户的默认地址");
        AddressBook addressBook = new AddressBook();
        addressBook = addressBookService.getDault();
        if(addressBook != null){
            return Result.success(addressBook);
        }
        return Result.error(MessageConstant.NO_DEFAULT_ADDRESS);
    }

    /**
     * 根据id修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation(value = "根据修改地址")
    public Result update(@RequestBody AddressBook addressBook){
        log.info("需要修改的地址主键为：{}",addressBook.getId());
        addressBookService.update(addressBook);
        return Result.success();
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "删除地址")
    public Result delete(Long id){
        log.info("需要删除的地址主键为：{}",id);
        addressBookService.deleteById(id);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询地址")
    public Result<AddressBook> getById(@PathVariable Long id){
        log.info("需要查询的地址id为：{}",id);
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation(value = "设置默认地址")
    public Result updateDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址为：{}",addressBook.getId());
        addressBookService.setDefault(addressBook);
        return Result.success();
    }


}
